package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.YlCheckInfoMapper;
import com.caifu.mapper.YlCostMapper;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.YlCheckInfo;
import com.caifu.pojo.YlCost;
import com.caifu.service.ICheckService;
import com.caifu.service.ICostService;
import com.caifu.util.Constance;
import com.caifu.util.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class CheckServiceImpl implements ICheckService {

    @Resource
    private YlCheckInfoMapper checkInfoMapper;

    @Override
    public JSONObject saveCheckInfo(YlCheckInfo checkInfo, SysUser user) throws Exception {
        JSONObject result = new JSONObject();
        if(null==user){
            result.put("code","1001");
            result.put("codeDesc","登录超时，请重新登录");
            return result;
        }
        if(null==checkInfo){
            result.put("code","1002");
            result.put("codeDesc","要保存的内容为空");
            return result;
        }
        try {
            //验证内容是否已存在
            String checkType = Constance.Null2EmptyTrim(checkInfo.getCheckType());
            String checkName = Constance.Null2EmptyTrim(checkInfo.getCheckName());
            String pensionNo = Constance.Null2EmptyTrim(user.getCurrentPensionNo());
            //验证名称是否已经存在
            YlCheckInfo oldCheckInfo = checkInfoMapper.queryCheckInfoByCheckName(checkName,checkType,pensionNo);
            if(null!=oldCheckInfo){
                result.put("code","1003");
                result.put("codeDesc","该名称已存在");
                return result;
            }
            checkInfo.setPensionNo(pensionNo);
            checkInfo.setTellerNo(user.getUserNo()+"");
            checkInfo.setCreateTime(Constance.currentDateTime());
            checkInfo.setStatus("1");
            checkInfoMapper.insert(checkInfo);
            result.put("code","0000");
            result.put("codeDesc","添加成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","添加失败");
            return result;
        }
    }

    @Override
    public JSONObject queryCheckInfoList(String checkType, SysUser user) throws Exception {
        JSONObject result = new JSONObject();
        JSONArray dataArray = new JSONArray();
        int checkCount = 0;
        if(null==user){
            result.put("code","0000");
            result.put("checkCount",checkCount);
            result.put("codeDesc","登录超时，请重新登录");
            return result;
        }
        try {
            QueryWrapper<YlCheckInfo> costQueryWrapper  = new QueryWrapper<>();
            costQueryWrapper.eq("CHECK_TYPE",checkType);
            costQueryWrapper.eq("PENSION_NO",user.getCurrentPensionNo());
            costQueryWrapper.eq("STATUS","1");
            costQueryWrapper.orderByAsc("CREATE_TIME");
            List<YlCheckInfo> checkInfoList = checkInfoMapper.selectList(costQueryWrapper);
            if(null!=checkInfoList&&checkInfoList.size()>0){
                checkCount =checkInfoList.size();
                for (int i=0;i<checkCount;i++) {
                    YlCheckInfo c = checkInfoList.get(i);
                    if(null!=c){
                        JSONObject data = new JSONObject();
                        data.put("checkNo",c.getCheckNo());
                        data.put("checkName",c.getCheckName());
                        data.put("order",i+1);
                        dataArray.add(data);
                    }
                }
            }
            result.put("code","0000");
            result.put("checkCount",checkCount);
            result.put("data",dataArray);
            result.put("codeDesc","查询成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","0000");
            result.put("costCount",0);
            result.put("codeDesc","查询失败");
            return result;
        }
    }

    @Override
    public JSONObject queryCheckInfoDetail(String checkNo) throws Exception {
        JSONObject result = new JSONObject();
        if(StringUtils.isBlank(checkNo)){
            result.put("code","1001");
            result.put("codeDesc","查询失败！");
            return result;
        }
        try {
            YlCheckInfo checkInfo = checkInfoMapper.selectById(checkNo);
            if(null==checkInfo){
                result.put("code","1001");
                result.put("codeDesc","查询失败！");
                return result;
            }
            result.put("checkName",checkInfo.getCheckName());
            result.put("checkNo",checkInfo.getCheckNo());
            result.put("checkType",checkInfo.getCheckType());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","1001");
            result.put("codeDesc","查询失败！");
            return result;
        }
    }

    @Override
    public JSONObject updateCheckInfo(YlCheckInfo checkInfo) throws Exception {
        JSONObject result = new JSONObject();
        if(null==checkInfo){
            result.put("code","1001");
            result.put("codeDesc","信息为空");
            return result;
        }
        try {
            //查询原楼层信息
            YlCheckInfo oldCheckInfo = checkInfoMapper.selectById(checkInfo.getCheckNo());
            if(null==oldCheckInfo){
                result.put("code","1002");
                result.put("codeDesc","原信息不存在！");
                return result;
            }
            String oldCheckName = Constance.Null2EmptyTrim(oldCheckInfo.getCheckName());
            String newCheckName = Constance.Null2EmptyTrim(checkInfo.getCheckName());
            String pensionNo = Constance.Null2EmptyTrim(oldCheckInfo.getPensionNo());
            String checkType = Constance.Null2EmptyTrim(oldCheckInfo.getCheckType());
            if(!oldCheckName.equals(newCheckName)){
                //验证楼层编号是否重复
                YlCheckInfo exitCheckInfo = checkInfoMapper
                        .queryCheckInfoByCheckName(newCheckName,checkType,pensionNo);
                if(null!=exitCheckInfo){
                    result.put("code","1003");
                    result.put("codeDesc","该名称已存在！");
                    return result;
                }
            }
            oldCheckInfo.setCheckName(newCheckName);
            oldCheckInfo.setCheckType(checkInfo.getCheckType());
            checkInfoMapper.updateById(oldCheckInfo);
            result.put("checkType",oldCheckInfo.getCheckType());
            result.put("code","0000");
            result.put("codeDesc","修改成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","修改失败！");
            return result;
        }
    }

    @Override
    public JSONObject deleteCheckInfo(String checkNo) throws Exception {
        JSONObject result = new JSONObject();
        if(StringUtils.isBlank(checkNo)){
            result.put("code","1001");
            result.put("codeDesc","删除失败！");
            return result;
        }
        try {
            YlCheckInfo checkInfo = checkInfoMapper.selectById(checkNo);
            if(null==checkInfo){
                result.put("code","1001");
                result.put("codeDesc","删除失败！");
                return result;
            }
            checkInfo.setStatus("9");
            checkInfoMapper.updateById(checkInfo);
            result.put("checkType",checkInfo.getCheckType());
            result.put("code","0000");
            result.put("codeDesc","删除成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","9999");
            result.put("codeDesc","删除失败！");
            return result;
        }
    }
}
